package com.moba.auth;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.moba.annotation.ParamValid;
import com.moba.constant.ConstantKey;
import com.moba.controller.BaseController;
import com.moba.domain.RoleUser;
import com.moba.domain.UserInfo;
import com.moba.emums.CodeType;
import com.moba.emums.IsDel;
import com.moba.security.JwtAuthenticationRequest;
import com.moba.security.JwtTokenUtil;
import com.moba.service.RoleUserService;
import com.moba.service.UserInfoService;
import com.moba.utils.ResultUtils;
import com.moba.utils.encrypt.DESPlusUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.security.core.AuthenticationException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
public class AuthController extends BaseController {
  private String tokenHeader = ConstantKey.JWT_HEADER;

  @Resource AuthService authService;

  @Resource UserInfoService userInfoService;

  @Resource RoleUserService roleUserService;

  @ParamValid
  @PostMapping(value = "/auth/login")
  public Object createAuthenticationToken(
      @RequestBody @Valid JwtAuthenticationRequest authenticationRequest, BindingResult result)
      throws AuthenticationException {

    JSONObject jsonObject = new JSONObject();
    DESPlusUtils des = null;
    try {
      des = new DESPlusUtils();

      UserInfo userInfo = new UserInfo();
      userInfo.setUserName(authenticationRequest.getUsername());
      userInfo.setPassword(des.encrypt(authenticationRequest.getPassword()));
      userInfo.setIsDel(0);
      int count = userInfoService.getEntityCount(userInfo);
      if (count == 0) {
        return ResultUtils.error(CodeType.CODE_TYPE_104, "手机号码未注册或密码不正确");
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

    final String token =
        authService.login(authenticationRequest.getUsername(), authenticationRequest.getPassword());

    // Return the token

    jsonObject.put("token", token);

    return ResultUtils.success(jsonObject);
  }

  @GetMapping(value = "/auth/refresh")
  public Object refreshAndGetAuthenticationToken(HttpServletRequest request)
      throws AuthenticationException {
    String token = request.getHeader(tokenHeader);
    String refreshedToken = authService.refresh(token);
    if (refreshedToken == null) {
      return ResultUtils.error(CodeType.CODE_TYPE_100.getIndex(), "刷新token失败");
    } else {
      return ResultUtils.success(null);
    }
  }

  @GetMapping(value = "/auth/remove")
  public Object removeAndGetAuthenticationToken(HttpServletRequest request)
      throws AuthenticationException {
    String token = request.getHeader(tokenHeader);
    Boolean flag = authService.remove(token);
    if (flag) {
      return ResultUtils.success(null);
    } else {
      return ResultUtils.error(CodeType.CODE_TYPE_100.getIndex(), "销毁token失败");
    }
  }

  @PostMapping(value = "/auth/register")
  public UserInfo register(@RequestBody UserInfo addedUser) throws AuthenticationException {
    return authService.register(addedUser);
  }

  @GetMapping(value = "/auth/token")
  public Object token(HttpServletRequest request, String username, String password)
      throws AuthenticationException {

    final String token = authService.login(username, password);

    JSONObject jsonObject = new JSONObject();
    jsonObject.put("token", token);

    return ResultUtils.success(jsonObject);
  }

  @GetMapping(value = "/auth/getUserByToken")
  public Object getUserByToken(HttpServletRequest request) {
    String token = request.getHeader(ConstantKey.JWT_HEADER);

    if (token == null || token.isEmpty()) {
      return ResultUtils.error(CodeType.CODE_TYPE_101.getIndex(), "Token为空，或参数名错误");
    }
    token = token.substring(ConstantKey.JWT_TOKEN_HEADER.length());
    JwtTokenUtil jwtTokenUtil = new JwtTokenUtil();
    String username = jwtTokenUtil.getUsernameFromToken(token);
    if (StringUtils.isBlank(username)) {
      return ResultUtils.error(CodeType.CODE_TYPE_102.getIndex(), CodeType.CODE_TYPE_102.getName());
    }

    UserInfo userInfo = new UserInfo();
    userInfo.setUserName(username);
    userInfo.setIsDel(IsDel.IS_DEL_0.getIndex());
    userInfo = userInfoService.getEntity(userInfo);

    JSONObject jsonObject = new JSONObject();
    jsonObject.put("userInfo", userInfo);

    RoleUser roleUser = new RoleUser();
    roleUser.setUserId(userInfo.getId());
    List<RoleUser> roleList = roleUserService.getEntityList(roleUser);
    JSONArray jsonArray = new JSONArray();
    if (null != roleList && roleList.size() > 0) {
      for (RoleUser temp : roleList) {
        jsonArray.add(temp.getRoleId());
      }
    }
    jsonObject.put("access", jsonArray);

    return ResultUtils.success(jsonObject);
  }
}
